package com.baymax.test.scene.drawable

import android.animation.Keyframe
import android.animation.ObjectAnimator
import android.animation.PropertyValuesHolder
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Rect
import android.graphics.drawable.GradientDrawable
import android.os.Build
import android.view.animation.LinearInterpolator
import androidx.annotation.RequiresApi
import com.badlogic.gdx.utils.Align.bottom
import com.badlogic.gdx.utils.Align.left
import com.badlogic.gdx.utils.Align.right
import com.badlogic.gdx.utils.Align.top
import com.baymax.test.scene.animation.RectEvaluator

class InvlateDrawable : GradientDrawable() {


    val pathR = Rect()
    val evaluator: RectEvaluator = RectEvaluator(pathR)

    init {
        setStroke(8, Color.GREEN)
        cornerRadius = 100f
    }

    override fun onBoundsChange(r: Rect) {
        super.onBoundsChange(r)
        val left = r.left
        val top  =r.top
        val right = r.right
        val bottom = r.bottom
        println("$left ,$top $right ,$bottom")
        pathR.set(0, 0, 10, bottom)

        val holder = PropertyValuesHolder.ofKeyframe(
            "rect",
            Keyframe.ofObject(0f, Rect(left, top, right, 10)), //t
            Keyframe.ofObject(0.25f, Rect(left, top, 10, bottom)),  //l
            Keyframe.ofObject(0.5f, Rect(left, bottom - 10, right, bottom)),//b
            Keyframe.ofObject(0.75f, Rect(right - 10, top, right, bottom)),//r
            Keyframe.ofObject(1f, Rect(left, top, right, 10)),
        )

        holder.setEvaluator(evaluator)
        ObjectAnimator.ofPropertyValuesHolder(pathR, holder).apply {
            addUpdateListener { invalidateSelf() }
            duration = 3000
            interpolator = LinearInterpolator()
            repeatCount = -1
            start()
        }


//            ObjectAnimator.ofObject(
//                evaluator,
//                Rect(left, top, right, 10), //top
//                Rect(left, top, 10, bottom), //left
//                Rect(left, bottom - 10, right, bottom), //bottom
//                Rect(right - 10, top, right, bottom),
//            ).apply {
//                addUpdateListener { invalidateSelf() }
//                duration = 3000
//                repeatCount = -1
//                start()
//            }
    }

    override fun draw(canvas: Canvas) {
        canvas.save()
        canvas.clipRect(pathR)
        super.draw(canvas)
        canvas.restore()
    }
}
